package com.ebt.m.proposal_v2.dao.request;

import android.text.TextUtils;

import com.ebt.m.proposal_v2.bean.ApplicantEntity;
import com.ebt.m.proposal_v2.bean.InsuranceEntity;
import com.ebt.m.proposal_v2.bean.InsuranceOption;
import com.ebt.m.proposal_v2.bean.InsuredEntity;
import com.ebt.m.proposal_v2.bean.ProposalEntity;
import com.ebt.m.proposal_v2.utils.DateUtils;
import com.ebt.m.proposal_v2.utils.EBTDataFormat;

import java.util.ArrayList;
import java.util.List;

/**
 * @author archer.qi
 *         Created on 2017/6/6.
 */
public class ProposalParam {

    /**
     * pickNum : abcd1234
     * proposalName : 计划书名称
     * description :
     * thumbnail :
     * totalCoverage : 100000
     * totalPremium : 1000
     * applicant : {"name":"投保人","sex":1,"age":30,"birthday":"1990-02-01","clientAccountId":""}
     * insured : [{"relation":1,"name":"被保人","sex":1,"age":30,"birthday":"1990-02-01","profession":1,"risk":[{"productId":3887,"productName":"产品名称","sort":0,"isMain":1,"thumbnail":"","featureData":[{"fieldName":"CoveragePeriod","value":-60,"displayName":"至60岁"},{"fieldName":"PaymentPeriod","value":5,"displayName":"5年交"},{"fieldName":"Premium","value":"4041.00","displayName":""},{"fieldName":"Coverage","value":100000,"displayName":""}]},{"productId":4102,"productName":"附加产品名称","sort":1,"isMain":0,"thumbnail":"","featureData":[{"fieldName":"CoveragePeriod","value":-60,"displayName":"至60岁"},{"fieldName":"PaymentPeriod","value":5,"displayName":"5年交"},{"fieldName":"Premium","value":"4041.00","displayName":""},{"fieldName":"Coverage","value":100000,"displayName":""}]}]}]
     */

    private String pickNum;
    private String proposalName;
    private String description;
    private String thumbnail;
    private String totalCoverage;
    private String totalPremium;
    private ApplicantBean applicant;
    private List<InsuredBean> insured;
    private int brandId;

    public int getBrandId() {
        return brandId;
    }

    public void setBrandId(int brandId) {
        this.brandId = brandId;
    }

    public String getPickNum() {
        return pickNum;
    }

    public void setPickNum(String pickNum) {
        this.pickNum = pickNum;
    }

    public String getProposalName() {
        return proposalName;
    }

    public void setProposalName(String proposalName) {
        this.proposalName = proposalName;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getThumbnail() {
        return thumbnail;
    }

    public void setThumbnail(String thumbnail) {
        this.thumbnail = thumbnail;
    }

    public String getTotalCoverage() {
        return totalCoverage;
    }

    public void setTotalCoverage(String totalCoverage) {
        this.totalCoverage = totalCoverage;
    }

    public String getTotalPremium() {
        return totalPremium;
    }

    public void setTotalPremium(String totalPremium) {
        this.totalPremium = totalPremium;
    }

    public ApplicantBean getApplicant() {
        return applicant;
    }

    public void setApplicant(ApplicantBean applicant) {
        this.applicant = applicant;
    }

    public List<InsuredBean> getInsured() {
        return insured;
    }

    public void setInsured(List<InsuredBean> insured) {
        this.insured = insured;
    }

    public static class ApplicantBean {
        /**
         * name : 投保人
         * sex : 1
         * age : 30
         * birthday : 1990-02-01
         * clientAccountId :
         */

        private String name;
        private int sex;
        private int age;
        private String birthday;
        private String clientAccountId;
        private String phone;

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getSex() {
            return sex;
        }

        public void setSex(int sex) {
            this.sex = sex;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getBirthday() {
            return birthday;
        }

        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }

        public String getClientAccountId() {
            return clientAccountId;
        }

        public void setClientAccountId(String clientAccountId) {
            this.clientAccountId = clientAccountId;
        }
    }

    public static class InsuredBean {
        /**
         * relation : 1
         * name : 被保人
         * sex : 1
         * age : 30
         * birthday : 1990-02-01
         * profession : 1
         * risk : [{"productId":3887,"productName":"产品名称","sort":0,"isMain":1,"thumbnail":"","featureData":[{"fieldName":"CoveragePeriod","value":-60,"displayName":"至60岁"},{"fieldName":"PaymentPeriod","value":5,"displayName":"5年交"},{"fieldName":"Premium","value":"4041.00","displayName":""},{"fieldName":"Coverage","value":100000,"displayName":""}]},{"productId":4102,"productName":"附加产品名称","sort":1,"isMain":0,"thumbnail":"","featureData":[{"fieldName":"CoveragePeriod","value":-60,"displayName":"至60岁"},{"fieldName":"PaymentPeriod","value":5,"displayName":"5年交"},{"fieldName":"Premium","value":"4041.00","displayName":""},{"fieldName":"Coverage","value":100000,"displayName":""}]}]
         */

        private int relation;
        private String name;
        private int sex;
        private int age;
        private String birthday;
        private int profession;
        private String clientAccountId;
        private List<RiskBean> risk;

        public String getClientAccountId() {
            return clientAccountId;
        }

        public void setClientAccountId(String clientAccountId) {
            this.clientAccountId = clientAccountId;
        }

        public int getRelation() {
            return relation;
        }

        public void setRelation(int relation) {
            this.relation = relation;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getSex() {
            return sex;
        }

        public void setSex(int sex) {
            this.sex = sex;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public String getBirthday() {
            return birthday;
        }

        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }

        public int getProfession() {
            return profession;
        }

        public void setProfession(int profession) {
            this.profession = profession;
        }

        public List<RiskBean> getRisk() {
            return risk;
        }

        public void setRisk(List<RiskBean> risk) {
            this.risk = risk;
        }

        public static class RiskBean {
            /**
             * productId : 3887
             * productName : 产品名称
             * sort : 0
             * isMain : 1
             * thumbnail :
             * featureData : [{"fieldName":"CoveragePeriod","value":-60,"displayName":"至60岁"},{"fieldName":"PaymentPeriod","value":5,"displayName":"5年交"},{"fieldName":"Premium","value":"4041.00","displayName":""},{"fieldName":"Coverage","value":100000,"displayName":""}]
             */

            private int productId;
            private String productName;
            private int sort;
            private int isMain;
            private String thumbnail;
            private String planCode;
            private List<InsuranceOption> featureData; // 产品属性，包括保额，保费等四个选项 + 其他选项
            private FeatureDataBean.ExtendJsonBean extendJson; // 豁免属性

            public FeatureDataBean.ExtendJsonBean getExtendJson() {
                return extendJson;
            }

            public void setExtendJson(FeatureDataBean.ExtendJsonBean extendJson) {
                this.extendJson = extendJson;
            }

            public int getProductId() {
                return productId;
            }

            public void setProductId(int productId) {
                this.productId = productId;
            }

            public String getProductName() {
                return productName;
            }

            public void setProductName(String productName) {
                this.productName = productName;
            }

            public int getSort() {
                return sort;
            }

            public void setSort(int sort) {
                this.sort = sort;
            }

            public int getIsMain() {
                return isMain;
            }

            public void setIsMain(int isMain) {
                this.isMain = isMain;
            }

            public String getThumbnail() {
                return thumbnail;
            }

            public void setThumbnail(String thumbnail) {
                this.thumbnail = thumbnail;
            }

            public List<InsuranceOption> getFeatureData() {
                return featureData;
            }

            public void setFeatureData(List<InsuranceOption> featureData) {
                this.featureData = featureData;
            }

            public String getPlanCode() {
                return planCode;
            }

            public void setPlanCode(String planCode) {
                this.planCode = planCode;
            }

            public static class FeatureDataBean {
                /**
                 * fieldName : CoveragePeriod
                 * value : -60
                 * displayName : 至60岁
                 */

                private String fieldName;
                private int value;
                private String displayName;

                public String getFieldName() {
                    return fieldName;
                }

                public void setFieldName(String fieldName) {
                    this.fieldName = fieldName;
                }

                public int getValue() {
                    return value;
                }

                public void setValue(int value) {
                    this.value = value;
                }

                public String getDisplayName() {
                    return displayName;
                }

                public void setDisplayName(String displayName) {
                    this.displayName = displayName;
                }

                public static class ExtendJsonBean {
                    private String planCode; // 产品编码，目前只应用于豁免险
                    private int riskType; // 区分豁免险和附加险， 豁免险 = 2
                    private String exemptPerson; // 固定 "Applicant" - 投保人 "Insured" - 被保人
                    private List<String> exemptProduct; // 豁免产品数组，存放planCode

                    public String getPlanCode() {
                        return planCode;
                    }

                    public void setPlanCode(String planCode) {
                        this.planCode = planCode;
                    }

                    public int getRiskType() {
                        return riskType;
                    }

                    public void setRiskType(int riskType) {
                        this.riskType = riskType;
                    }

                    public String getExemptPerson() {
                        return exemptPerson;
                    }

                    public void setExemptPerson(String exemptPerson) {
                        this.exemptPerson = exemptPerson;
                    }

                    public List<String> getExemptProduct() {
                        return exemptProduct;
                    }

                    public void setExemptProduct(List<String> exemptProduct) {
                        this.exemptProduct = exemptProduct;
                    }
                }
            }
        }
    }

    /**
     * ProposalEntity参数化
     *
     * @param proposal
     * @return
     */
    public static ProposalParam param(ProposalEntity proposal) {
        if (proposal == null) {
            return null;
        }
        ProposalParam param = new ProposalParam();
        param.setPickNum(proposal.pickNum);
        param.setProposalName(proposal.proposalName);
        param.setDescription(proposal.description);
        param.setThumbnail(proposal.thumbnail);

        param.setTotalCoverage(EBTDataFormat.formatNumber(proposal.totalCoverage));
        param.setTotalPremium(EBTDataFormat.formatNumber(proposal.totalPremium));

        ApplicantBean applicantBean = new ApplicantBean();
        if (proposal.applicant != null) {
            applicantBean.setClientAccountId(proposal.applicant.clientAccountId == null ? "" : proposal.applicant.clientAccountId);
            applicantBean.setName(proposal.applicant.name);
            applicantBean.setSex(proposal.applicant.sex);
            applicantBean.setAge(proposal.applicant.age);
            if (TextUtils.isEmpty(proposal.applicant.birthday)) {
                applicantBean.setBirthday(DateUtils.getBirthdayByAge(proposal.applicant.age, "-"));
            } else {
                applicantBean.setBirthday(proposal.applicant.birthday);
            }
            applicantBean.setPhone(proposal.applicant.phone);
        }
        param.setApplicant(applicantBean);

        List<InsuredBean> insuredBeenList = new ArrayList<>();
        if (proposal.insured != null && proposal.insured.size() > 0) {
            for (InsuredEntity entity : proposal.insured) {
                if (entity != null) {
                    InsuredBean insuredBean = new InsuredBean();
                    insuredBean.setClientAccountId(entity.clientAccountId == null ? "" : entity.clientAccountId);
                    insuredBean.setName(entity.name);
                    insuredBean.setSex(entity.sex);
                    insuredBean.setAge(entity.age);
                    if (TextUtils.isEmpty(entity.birthday)) {
                        insuredBean.setBirthday(DateUtils.getBirthdayByAge(entity.age, "-"));
                    } else {
                        insuredBean.setBirthday(entity.birthday);
                    }
                    insuredBean.setRelation(entity.relation);
                    insuredBean.setProfession(entity.profession);

                    List<InsuredBean.RiskBean> riskBeanList = new ArrayList<>();
                    if (entity.risk != null && entity.risk.size() > 0) {
                        for (InsuranceEntity risk : entity.risk) {
                            if (risk != null) {
                                if (risk.isMain == 1) { // 计划书的brandId取主险的brandId
                                    param.setBrandId(risk.brandId);
                                }
                                InsuredBean.RiskBean riskBean = new InsuredBean.RiskBean();
                                riskBean.setProductId(risk.productId);
                                riskBean.setProductName(risk.productName);
                                if (risk.isBindingMain && risk.isMain != 1) {

                                    /* 非主险 & 绑定主险 -> isMain为2*/
                                    riskBean.setIsMain(2);
                                } else {
                                    riskBean.setIsMain(risk.isMain);
                                }
                                riskBean.setThumbnail(risk.thumbnail);
                                riskBean.setSort(risk.sort);
                                /* 先调用reparseFeatureData赋值*/
                                risk.reparseFeatureData();
                                riskBean.setFeatureData(risk.featureData);
                                riskBean.setPlanCode(risk.planCode);
                                      /* 赋值豁免险模块*/
                                InsuredBean.RiskBean.FeatureDataBean.ExtendJsonBean extendJsonBean = new InsuredBean.RiskBean.FeatureDataBean.ExtendJsonBean();
                                extendJsonBean.setPlanCode(risk.planCode);
                                extendJsonBean.setRiskType(risk.riskType);
                                extendJsonBean.setExemptPerson(risk.exemptPerson);
                                extendJsonBean.setExemptProduct(risk.exemptProduct);
                                riskBean.setExtendJson(extendJsonBean);

                                riskBeanList.add(riskBean);
                            }
                        }
                    }
                    insuredBean.setRisk(riskBeanList);

                    insuredBeenList.add(insuredBean);
                }
            }
        }
        param.setInsured(insuredBeenList);
        return param;
    }

    /**
     * 投保人
     *
     * @return
     */
    public ApplicantEntity getProposalApplicant() {
        if (applicant == null) {
            return null;
        }
        ApplicantEntity mApplicant = new ApplicantEntity();
        mApplicant.clientAccountId = applicant.getClientAccountId();
        mApplicant.name = applicant.getName();
        mApplicant.sex = applicant.getSex();
        mApplicant.age = applicant.getAge();
        mApplicant.birthday = applicant.getBirthday();
        return mApplicant;
    }

    /**
     * 被保人<单被保人版本>
     *
     * @return
     */
    public InsuredEntity getProposalInsured() {
        if (insured == null || insured.size() == 0) {
            return null;
        }
        InsuredBean insuredBean = insured.get(0);
        if (insuredBean == null) {
            return null;
        }
        InsuredEntity mInsured = new InsuredEntity();
        mInsured.clientAccountId = insuredBean.getClientAccountId();
        mInsured.name = insuredBean.getName();
        mInsured.sex = insuredBean.getSex();
        mInsured.age = insuredBean.getAge();
        mInsured.birthday = insuredBean.getBirthday();
        mInsured.profession = insuredBean.getProfession();
        mInsured.relation = insuredBean.getRelation();
        return mInsured;
    }

    /**
     * 险种<单被保人版本>
     *
     * @return
     */
    public List<InsuranceEntity> getProposalRisks() {
        if (insured == null || insured.size() == 0) {
            return null;
        }
        InsuredBean insuredBean = insured.get(0);
        if (insuredBean == null) {
            return null;
        }
        List<InsuredBean.RiskBean> risks = insuredBean.getRisk();
        if (risks == null || risks.size() == 0) {
            return null;
        }
        List<InsuranceEntity> mInsurances = new ArrayList<>();
        for (InsuredBean.RiskBean riskBean : risks) {
            if (riskBean != null) {
                InsuranceEntity insurance = new InsuranceEntity();
                insurance.productId = riskBean.getProductId();
                insurance.productName = riskBean.getProductName();
                insurance.thumbnail = riskBean.getThumbnail();
                insurance.isMain = riskBean.getIsMain();
                if (riskBean.getIsMain() == 2) {
                    insurance.isBindingMain = true;
                }
                insurance.sort = riskBean.getSort();
                insurance.featureData = riskBean.getFeatureData();
                /* 将险种参数从featureData中解析到对应的InsuranceOption中*/
                insurance.parseFeatureData();
                /* 添加从属被保人ID*/
                insurance.addInsuredIdBelong(insuredBean.getClientAccountId());
                mInsurances.add(insurance);
            }
        }
        return mInsurances;
    }
}
